package com.animatedpicture.object;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.geom.Rectangle2D;
import java.util.Random;

public class Light implements IMovingObject{

	private static final double HOLDER_WIDTH = 3.0;
	private static final double HOLDER_LENGTH = 20.0;
	
	private static final double COLUMN_WIDTH = 5.0;
	private static final double COLUMN_LENGTH = 3.0;
	
	private static Boolean animateCheck = false;
	
	private Point objPosition;
	
	private Random mRandom = new Random();
	
	public Light()
	{
		objPosition = null;
	}
	
	@Override
	public void draw(Graphics2D g2) {
		
		if (objPosition != null)
		{
			Graphics2D holderG = g2;
			
			Rectangle2D holder = new Rectangle2D.Double(objPosition.x, 0, HOLDER_LENGTH, HOLDER_WIDTH);
			
			holderG.setColor(Color.BLACK);
			holderG.fill(holder);
			
			Graphics2D columnG = g2;
			
			Rectangle2D column = new Rectangle2D.Double(objPosition.x + (HOLDER_LENGTH/2 - COLUMN_LENGTH/2), 
														HOLDER_WIDTH, 
														COLUMN_LENGTH, 
														COLUMN_WIDTH);
			
			columnG.setColor(Color.BLACK);
			columnG.fill(column);
			
			Graphics2D bodyG = g2;
			
			Rectangle2D body = new Rectangle2D.Double(objPosition.x, 
													  HOLDER_WIDTH + COLUMN_WIDTH, 
													  HOLDER_LENGTH, 
													  HOLDER_LENGTH);
			
			bodyG.setColor(Color.BLACK);
			bodyG.fill(body);
			
			if (animateCheck)
			{
				Graphics2D lightsG = g2;
				
				int[] xpoints = {objPosition.x, 
							     (int)(objPosition.x+HOLDER_LENGTH), 
							     (int)(objPosition.x+HOLDER_LENGTH)+50,
							     objPosition.x - 50};
				
				int[] ypoints = {(int)(HOLDER_WIDTH + COLUMN_WIDTH + HOLDER_LENGTH),
								 (int)(HOLDER_WIDTH + COLUMN_WIDTH + HOLDER_LENGTH),
								 700 - (int)(HOLDER_WIDTH + COLUMN_WIDTH + HOLDER_LENGTH),
								 700 - (int)(HOLDER_WIDTH + COLUMN_WIDTH + HOLDER_LENGTH)};
				
				Polygon light = new Polygon(xpoints, ypoints, xpoints.length);
				
				lightsG.setColor(new Color(mRandom.nextInt(256),mRandom.nextInt(256),mRandom.nextInt(256), 64));
				lightsG.fill(light);
				
			}
			
		}
		
	}

	@Override
	public void setPosition(Point mPoint) {
		objPosition = mPoint;
		System.out.println("ObjectCar SetPosition : ("+mPoint.x+","+mPoint.y+")");
	}

	@Override
	public void animate(boolean trriger) {
		animateCheck = trriger;
	}

	@Override
	public void playMusic(boolean trriger) {
		// Do not play music in this class.
	}

}
